/*     */ package com.zimbra.cs.account.ldap.custom;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.account.ldap.SpecialAttrs;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CustomLdapDIT
/*     */   extends LdapDIT
/*     */ {
/*  65 */   private final String DEFAULT_BASE_RDN_DOMAIN = "cn=domains";
/*     */   private String BASE_DN_DOMAIN;
/*     */   
/*     */   public CustomLdapDIT(LdapProv prov) {
/*  69 */     super(prov);
/*     */   }
/*     */   
/*     */   private String getLC(KnownKey key, String defaultValue) {
/*  73 */     String lcValue = key.value();
/*     */     
/*  75 */     if (StringUtil.isNullOrEmpty(lcValue)) {
/*  76 */       return defaultValue;
/*     */     }
/*  78 */     return lcValue;
/*     */   }
/*     */   
/*     */   private String getLCAndValidateUnderConfigBranchDN(KnownKey key, String defaultValue) {
/*  82 */     String dn = getLC(key, defaultValue);
/*     */     
/*  84 */     if (!validateUnderDN(this.BASE_DN_CONFIG_BRANCH, dn)) {
/*  85 */       ZimbraLog.account.warn("dn " + dn + " must be under " + this.BASE_DN_CONFIG_BRANCH + ", localconfig value " + dn + " ignored, using default value " + defaultValue);
/*  86 */       dn = defaultValue;
/*     */     }
/*     */     
/*  89 */     return dn;
/*     */   }
/*     */   
/*     */   protected void init()
/*     */   {
/*  94 */     this.BASE_DN_CONFIG_BRANCH = getLC(LC.ldap_dit_base_dn_config, "cn=zimbra");
/*  95 */     this.BASE_DN_MAIL_BRANCH = getLC(LC.ldap_dit_base_dn_mail, "").toLowerCase();
/*     */     
/*  97 */     this.BASE_RDN_ACCOUNT = "";
/*  98 */     this.BASE_RDN_DYNAMICGROUP = "";
/*     */     
/* 100 */     this.NAMING_RDN_ATTR_USER = getLC(LC.ldap_dit_naming_rdn_attr_user, "uid");
/* 101 */     this.NAMING_RDN_ATTR_COS = getLC(LC.ldap_dit_naming_rdn_attr_cos, "cn");
/* 102 */     this.NAMING_RDN_ATTR_DYNAMICGROUP = getLC(LC.ldap_dit_naming_rdn_attr_dynamicgroup, "cn");
/* 103 */     this.NAMING_RDN_ATTR_GLOBALCONFIG = getLC(LC.ldap_dit_naming_rdn_attr_globalconfig, "cn");
/* 104 */     this.NAMING_RDN_ATTR_GLOBALGRANT = getLC(LC.ldap_dit_naming_rdn_attr_globalgrant, "cn");
/* 105 */     this.NAMING_RDN_ATTR_MIME = getLC(LC.ldap_dit_naming_rdn_attr_mime, "cn");
/* 106 */     this.NAMING_RDN_ATTR_SERVER = getLC(LC.ldap_dit_naming_rdn_attr_server, "cn");
/* 107 */     this.NAMING_RDN_ATTR_ALWAYSONCLUSTER = getLC(LC.ldap_dit_base_dn_alwaysoncluster, "cn");
/* 108 */     this.NAMING_RDN_ATTR_UCSERVICE = getLC(LC.ldap_dit_naming_rdn_attr_ucservice, "cn");
/* 109 */     this.NAMING_RDN_ATTR_SHARE_LOCATOR = getLC(LC.ldap_dit_naming_rdn_attr_share_locator, "cn");
/* 110 */     this.NAMING_RDN_ATTR_XMPPCOMPONENT = getLC(LC.ldap_dit_naming_rdn_attr_xmppcomponent, "cn");
/* 111 */     this.NAMING_RDN_ATTR_ZIMLET = getLC(LC.ldap_dit_naming_rdn_attr_zimlet, "cn");
/*     */     
/* 113 */     this.DN_GLOBALCONFIG = (this.NAMING_RDN_ATTR_GLOBALCONFIG + "=config" + "," + this.BASE_DN_CONFIG_BRANCH);
/* 114 */     this.DN_GLOBALGRANT = (this.NAMING_RDN_ATTR_GLOBALGRANT + "=globalgrant" + "," + this.BASE_DN_CONFIG_BRANCH);
/*     */     
/* 116 */     this.BASE_DN_ADMIN = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_admin, "cn=admins," + this.BASE_DN_CONFIG_BRANCH);
/* 117 */     this.BASE_DN_APPADMIN = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_appadmin, "cn=appaccts," + this.BASE_DN_CONFIG_BRANCH);
/* 118 */     this.BASE_DN_COS = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_cos, "cn=cos," + this.BASE_DN_CONFIG_BRANCH);
/* 119 */     this.BASE_DN_GLOBAL_DYNAMICGROUP = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_global_dynamicgroup, "cn=groups," + this.BASE_DN_CONFIG_BRANCH);
/*     */     
/* 121 */     this.BASE_DN_MIME = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_mime, "cn=mime," + this.DN_GLOBALCONFIG);
/* 122 */     this.BASE_DN_SERVER = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_server, "cn=servers," + this.BASE_DN_CONFIG_BRANCH);
/* 123 */     this.BASE_DN_ALWAYSONCLUSTER = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_alwaysoncluster, "cn=alwaysOnClusters," + this.BASE_DN_CONFIG_BRANCH);
/* 124 */     this.BASE_DN_UCSERVICE = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_ucservice, "cn=ucservices," + this.BASE_DN_CONFIG_BRANCH);
/* 125 */     this.BASE_DN_SHARE_LOCATOR = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_share_locator, "cn=sharelocators," + this.BASE_DN_CONFIG_BRANCH);
/* 126 */     this.BASE_DN_XMPPCOMPONENT = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_xmppcomponent, "cn=xmppcomponents," + this.BASE_DN_CONFIG_BRANCH);
/* 127 */     this.BASE_DN_ZIMLET = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_zimlet, "cn=zimlets," + this.BASE_DN_CONFIG_BRANCH);
/*     */     
/* 129 */     this.BASE_DN_DOMAIN = getLCAndValidateUnderConfigBranchDN(LC.ldap_dit_base_dn_domain, "cn=domains," + this.BASE_DN_CONFIG_BRANCH);
/* 130 */     this.BASE_DN_ZIMBRA = computeZimbraBaseDN();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private String computeZimbraBaseDN()
/*     */   {
/* 137 */     String[] rdns1 = this.BASE_DN_CONFIG_BRANCH.split(",");
/* 138 */     String[] rdns2 = this.BASE_DN_MAIL_BRANCH.split(",");
/*     */     
/* 140 */     int idx1 = rdns1.length - 1;
/* 141 */     int idx2 = rdns2.length - 1;
/*     */     
/* 143 */     int shorter = Math.min(rdns1.length, rdns2.length);
/*     */     
/* 145 */     String commonDn = "";
/* 146 */     for (int i = 0; i < shorter; idx2--) {
/* 147 */       if (!rdns1[idx1].equalsIgnoreCase(rdns2[idx2])) break;
/* 148 */       if (commonDn.isEmpty()) {
/* 149 */         commonDn = rdns1[idx1];
/*     */       } else {
/* 151 */         commonDn = rdns1[idx1] + "," + commonDn;
/*     */       }
/* 146 */       i++;idx1--;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 157 */     return commonDn;
/*     */   }
/*     */   
/*     */   private ServiceException UNSUPPORTED(String msg) {
/* 161 */     return ServiceException.FAILURE(msg + " unsupported in " + getClass().getCanonicalName(), null);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private boolean validateUnderDN(String parentDn, String dn)
/*     */   {
/* 168 */     return isUnder(parentDn, dn);
/*     */   }
/*     */   
/*     */   private void validateMailBranchEntryDN(String dn) throws ServiceException {
/* 172 */     if (!validateUnderDN(this.BASE_DN_MAIL_BRANCH, dn))
/* 173 */       throw ServiceException.INVALID_REQUEST("dn " + dn + " must be under " + this.BASE_DN_MAIL_BRANCH, null);
/*     */   }
/*     */   
/*     */   private String defaultDomain() throws ServiceException {
/* 177 */     String defaultDomain = this.mProv.getConfig().getAttr("zimbraDefaultDomainName", null);
/* 178 */     if (StringUtil.isNullOrEmpty(defaultDomain)) {
/* 179 */       throw UNSUPPORTED("default domain is empty");
/*     */     }
/* 181 */     return defaultDomain;
/*     */   }
/*     */   
/*     */   private String acctAndDLDNCreate(String baseDn, IAttributes attrs) throws ServiceException {
/* 185 */     String rdnAttr = this.NAMING_RDN_ATTR_USER;
/* 186 */     String rdnValue = attrs.getAttrString(rdnAttr);
/*     */     
/* 188 */     if (rdnValue == null) {
/* 189 */       throw ServiceException.FAILURE("missing rdn attribute" + rdnAttr, null);
/*     */     }
/* 191 */     validateMailBranchEntryDN(baseDn);
/*     */     
/* 193 */     return rdnAttr + "=" + LdapUtil.escapeRDNValue(rdnValue) + "," + baseDn;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String emailToDN(String localPart, String domain)
/*     */     throws ServiceException
/*     */   {
/* 203 */     throw UNSUPPORTED("function emailToDN");
/*     */   }
/*     */   
/*     */   public String emailToDN(String email) throws ServiceException
/*     */   {
/* 208 */     throw UNSUPPORTED("function emailToDN");
/*     */   }
/*     */   
/*     */   public String accountDNCreate(String baseDn, IAttributes attrs, String localPart, String domain) throws ServiceException
/*     */   {
/* 213 */     if (baseDn == null) {
/* 214 */       throw ServiceException.INVALID_REQUEST("base dn is required in DIT impl " + getClass().getCanonicalName(), null);
/*     */     }
/* 216 */     return acctAndDLDNCreate(baseDn, attrs);
/*     */   }
/*     */   
/*     */   public String accountDNRename(String oldDn, String newLocalPart, String newDomain) throws ServiceException
/*     */   {
/* 221 */     return oldDn;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String dnToEmail(String dn, IAttributes attrs)
/*     */     throws ServiceException
/*     */   {
/* 229 */     String localPart = attrs.getAttrString("uid");
/*     */     
/* 231 */     if (localPart != null) {
/* 232 */       return localPart + "@" + defaultDomain();
/*     */     }
/* 234 */     throw ServiceException.FAILURE("unable to map dn [" + dn + "] to email", null);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String aliasDN(String targetDn, String targetDomain, String aliasLocalPart, String aliasDomain)
/*     */     throws ServiceException
/*     */   {
/* 261 */     if ((targetDn == null) || (targetDomain == null)) {
/* 262 */       throw UNSUPPORTED("alias DN without target dn or target domain");
/*     */     }
/* 264 */     String allowedDomain = defaultDomain();
/* 265 */     if (!aliasDomain.equals(allowedDomain)) {
/* 266 */       throw UNSUPPORTED("alias DN not in default domain(alias domain=" + aliasDomain + ", default domain=" + allowedDomain + ")");
/*     */     }
/* 268 */     if (!targetDomain.equals(aliasDomain)) {
/* 269 */       throw UNSUPPORTED("alias DN with different target domain and alias domain(alias domain=" + aliasDomain + ", target domain=" + targetDomain + ")");
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 274 */     String[] parts = LdapUtil.dnToRdnAndBaseDn(targetDn);
/* 275 */     return this.NAMING_RDN_ATTR_USER + "=" + LdapUtil.escapeRDNValue(aliasLocalPart) + "," + parts[1];
/*     */   }
/*     */   
/*     */   public String aliasDNRename(String targetNewDn, String targetNewDomain, String newAliasEmail) throws ServiceException
/*     */   {
/* 280 */     if ((targetNewDn == null) || (targetNewDomain == null)) {
/* 281 */       throw UNSUPPORTED("alias DN rename without target dn or target domain");
/*     */     }
/* 283 */     String allowedDomain = defaultDomain();
/* 284 */     if (!targetNewDomain.equals(allowedDomain)) {
/* 285 */       throw UNSUPPORTED("alias DN rename not in default domain(alias domain=" + targetNewDomain + ", default domain=" + allowedDomain + ")");
/*     */     }
/* 287 */     return targetNewDn;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String distributionListDNCreate(String baseDn, IAttributes attrs, String localPart, String domain)
/*     */     throws ServiceException
/*     */   {
/* 306 */     if (baseDn == null) {
/* 307 */       throw ServiceException.INVALID_REQUEST("base dn is required in DIT impl " + getClass().getCanonicalName(), null);
/*     */     }
/* 309 */     String allowedDomain = defaultDomain();
/* 310 */     if (!domain.equals(allowedDomain)) {
/* 311 */       throw UNSUPPORTED("DL DN not in default domain(DL domain=" + domain + ", default domain=" + allowedDomain + ")");
/*     */     }
/* 313 */     return acctAndDLDNCreate(baseDn, attrs);
/*     */   }
/*     */   
/*     */   public String distributionListDNRename(String oldDn, String newLocalPart, String newDomain) throws ServiceException
/*     */   {
/* 318 */     return oldDn;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String domainBaseDN()
/*     */   {
/* 329 */     return this.BASE_DN_DOMAIN;
/*     */   }
/*     */   
/*     */   public String[] domainToDNs(String[] parts)
/*     */   {
/* 334 */     return domainToDNsInternal(parts, this.BASE_DN_DOMAIN);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String domainToAccountSearchDN(String domain)
/*     */     throws ServiceException
/*     */   {
/* 346 */     return this.BASE_DN_MAIL_BRANCH;
/*     */   }
/*     */   
/*     */   public String domainDNToAccountSearchDN(String domainDN)
/*     */     throws ServiceException
/*     */   {
/* 352 */     return this.BASE_DN_MAIL_BRANCH;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZLdapFilter filterAccountsByDomainAndServer(Domain domain, Server server)
/*     */   {
/* 363 */     return ZLdapFilterFactory.getInstance().velodromeAllAccountsByDomainAndServer(domain.getName(), server.getServiceHostname());
/*     */   }
/*     */   
/*     */ 
/*     */   public ZLdapFilter filterAccountsOnlyByDomainAndServer(Domain domain, Server server)
/*     */   {
/* 369 */     return ZLdapFilterFactory.getInstance().velodromeAllAccountsOnlyByDomainAndServer(domain.getName(), server.getServiceHostname());
/*     */   }
/*     */   
/*     */ 
/*     */   public ZLdapFilter filterCalendarResourceByDomainAndServer(Domain domain, Server server)
/*     */   {
/* 375 */     return ZLdapFilterFactory.getInstance().velodromeAllCalendarResourcesByDomainAndServer(domain.getName(), server.getServiceHostname());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public ZLdapFilter filterAccountsOnlyByDomain(Domain domain)
/*     */   {
/* 382 */     return ZLdapFilterFactory.getInstance().velodromeAllAccountsOnlyByDomain(domain.getName());
/*     */   }
/*     */   
/*     */ 
/*     */   public ZLdapFilter filterCalendarResourcesByDomain(Domain domain)
/*     */   {
/* 388 */     return ZLdapFilterFactory.getInstance().velodromeAllCalendarResourcesByDomain(domain.getName());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZLdapFilter filterDistributionListsByDomain(Domain domain)
/*     */   {
/* 399 */     return ZLdapFilterFactory.getInstance().velodromeAllDistributionListsByDomain(domain.getName());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ZLdapFilter filterGroupsByDomain(Domain domain)
/*     */   {
/* 410 */     return ZLdapFilterFactory.getInstance().velodromeAllGroupsByDomain(domain.getName());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public SpecialAttrs handleSpecialAttrs(Map<String, Object> attrs)
/*     */     throws ServiceException
/*     */   {
/* 422 */     if (SpecialAttrs.getSingleValuedAttr(attrs, "ldap.baseDN") == null) {
/* 423 */       throw ServiceException.INVALID_REQUEST("missing required attribute ldap.baseDN", null);
/*     */     }
/* 425 */     if ((!this.NAMING_RDN_ATTR_USER.equals("uid")) && 
/* 426 */       (SpecialAttrs.getSingleValuedAttr(attrs, this.NAMING_RDN_ATTR_USER) == null)) {
/* 427 */       throw ServiceException.INVALID_REQUEST("missing required attribute " + this.NAMING_RDN_ATTR_USER, null);
/*     */     }
/*     */     
/* 430 */     SpecialAttrs specialAttrs = new SpecialAttrs();
/* 431 */     if (attrs != null) {
/* 432 */       specialAttrs.handleZimbraId(attrs);
/* 433 */       specialAttrs.handleLdapBaseDn(attrs);
/*     */     }
/*     */     
/*     */ 
/* 437 */     return specialAttrs;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/custom/CustomLdapDIT.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */